package com.wenjiang.service.impl;

import com.wenjiang.common.dto.ChangeRoleResourcesParam;
import com.wenjiang.common.lang.Result;
import com.wenjiang.mapper.MallPermissionMapper;
import com.wenjiang.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    MallPermissionMapper mallPermissionMapper;

    // 获取所有资源
    @Override
    public Result getAllPermission() {
        return Result.succ(mallPermissionMapper.getAllResources());
    }

    // 修改角色可操作的后台资源
    @Override
    public Result changeRoleResource(ChangeRoleResourcesParam changeRoleResourcesParam) {
        // 先清空角色之前资源权限
        mallPermissionMapper.clearRoleResource(changeRoleResourcesParam.getRoleId());

        // 判断是否为空
        if(!changeRoleResourcesParam.getPermIds().isEmpty()){
            // 然后进行添加
            mallPermissionMapper.addRoleResource(changeRoleResourcesParam.getRoleId(), changeRoleResourcesParam.getPermIds());
        }



        return Result.succ("修改成功");
    }
}
